package com.example.itensyuan0609;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.example.itensyuan0609.fragment.OneFragment;
import com.example.itensyuan0609.fragment.ThreeFragment;
import com.example.itensyuan0609.fragment.TwoFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
    private TextView title;
    private ViewPager page;
    private RadioButton one;
    private RadioButton two;
    private RadioButton three;
    private RadioGroup group;
    private List<Fragment> list;
    private VpageAdapter vadapter;
    private FragmentManager manager;
    String[] titles = new String[]{"首页", "设置", "我的"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        addfram();
    }

    public void init() {
        title = findViewById(R.id.tv_title);
        page = findViewById(R.id.page);
        one = findViewById(R.id.wx);
        two = findViewById(R.id.fx);
        three = findViewById(R.id.wd);
        group = findViewById(R.id.group);
        manager = getSupportFragmentManager();
        group.setOnCheckedChangeListener(this);
    }

    public void addfram() {
        list = new ArrayList<>();
        OneFragment one = new OneFragment();
        TwoFragment two = new TwoFragment();
        ThreeFragment three = new ThreeFragment();
        list.add(one);
        list.add(two);
        list.add(three);
        vadapter = new VpageAdapter(manager, 0, list);
        page.setAdapter(vadapter);
        page.setCurrentItem(0);
        page.setOnPageChangeListener(this);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.wx:
                page.setCurrentItem(0);
                title.setText(titles[0]);
                title.setVisibility(View.GONE);
                break;
            case R.id.fx:
                page.setCurrentItem(1);
                title.setText(titles[1]);
                title.setVisibility(View.GONE);
                break;
            case R.id.wd:
                page.setCurrentItem(2);
                title.setVisibility(View.GONE);
                title.setText(titles[2]);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        title.setText(titles[position]);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == 2) {
            switch (page.getCurrentItem()) {
                case 0:
                    one.setChecked(true);
                    title.setVisibility(View.GONE);
                    break;
                case 1:
                    two.setChecked(true);
                    title.setVisibility(View.GONE);
                    break;
                case 2:
                    three.setChecked(true);
                    title.setVisibility(View.GONE);
                    break;
            }
        }
    }
}
